<%@ page language="java" import="java.util.*" pageEncoding="UTF-8"%>
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>

<%
String path = request.getContextPath();
String basePath = request.getScheme()+"://"+request.getServerName()+":"+request.getServerPort()+path+"/";
%>

<!DOCTYPE HTML>
<html lang="zh-CN">
   <head>
	 <meta charset="utf-8">
	 <meta http-equiv="X-UA-Compatible" content="IE=edge">
	 <meta name="viewport" content="width=device-width, initial-scale=1">
   </head>
   <body>
	<div class="modal-content">
	  <div class="modal-header">
	    <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
	    <h4 class="modal-title" id="myModalLabel"><c:if test="${type==1}">新增</c:if><c:if test="${type==2}">编辑</c:if></h4>
	  </div>
	  <div class="modal-body">
	
	    <form id="objectForm" class="form-horizontal" method="post">
	    	<c:if test="${type==2}">
		      	<input type="hidden" name="token" value="${wyAccount.token}"/>
		      	<input type="hidden" name="weChat" value="${wyAccount.weChat}"/>
		      	<input type="hidden" id="loginTime" name="loginTime" value="${wyAccount.loginTime}"/>
		      	<input type="hidden" name="talkbackUUID" value="${wyAccount.talkbackUUID}"/>
		      	<input type="hidden" name="isFirstLogin" value="${wyAccount.isFirstLogin}"/>
	      	</c:if>
	      <div class="form-group">
		        <label for="name" class="col-sm-2 control-label"><span style="color:red;"> * </span>工号</label>
		        <div class="col-sm-6">
		          <input type="text" class="form-control" name="workCode" value="${wyAccount.workCode}"/>
		        </div>
	      </div> 
	      <div class="form-group">
	      	<input type="hidden" name="id" value="${wyAccount.id}"/>
	        <input type="hidden" class="typeCode" value="${type}"/>
	        <label for="name" class="col-sm-2 control-label"><span style="color:red;"> * </span>电话号码</label>
	        <div class="col-sm-6">
	          <input type="text" class="form-control" id="name" name="name" value="${wyAccount.name}">
	        </div>
	      </div> 
	      <div class="form-group">
	        <label for="code" class="col-sm-2 control-label"><span style="color:red;"> * </span>密码</label>
	        <div class="col-sm-6">
	          <input type="password" class="form-control" id="password" name="password" value="${wyAccount.password}">
	        </div>
	      </div>
		<div class="form-group">
			<label for="content" class="col-sm-2 control-label">所属角色</label>
			<div class="col-sm-10">
				<c:if test="${type==2}"><input type="hidden" id="wyRoleEdit" value="${wyAccount.wyRole.id}"></c:if>
				<select class="form-control combox" id="wyRole"
					name="wyRole[id]" data-live-search="true">
					<option value="">---请选择---</option>
					<c:forEach items="${wyRoleList}" var="wyRole">
						<option value="${wyRole.id}">${wyRole.name}</option>
					</c:forEach>
				</select>
			</div>
		</div>
		
		<div class="form-group">
	        <label for="content" class="col-sm-2 control-label">分配项目</label>
	        <div class="col-sm-10"> 
	        	<select class="form-control combox"  id="projectIds" data-live-search="true" multiple>
		         	<c:forEach items="${projectList}" var ="project">
		             	 <option value="${project.id}">${project.name}</option>
		         	</c:forEach>
		      	</select> 
	        </div>
	    </div> 
	       
		</form>
	  </div>
	  <div class="modal-footer">
	  <div class="form-group">
	    <button type="button" class="btn btn-info btn-white btn-round" onclick="objectSubmit();"> <i class="glyphicon glyphicon-ok bigger-125"></i> 确认</button>
	    <button type="button" class="btn btn-default btn-white btn-round" data-dismiss="modal"><i class="glyphicon glyphicon-remove bigger-125"></i> 取消</button>
	  </div>
	  </div>
	</div>
	
	<script>
	 var path = "<%=path%>";
	 $(function(){
		
   	   if($(".typeCode").val()==2){
   		   //编辑状态下显示已保存的角色
   		   var wyRole = $("#wyRoleEdit").val();
	       $('#wyRole').select2({width:'270px'}).val(wyRole).trigger("change");
	       
	     //编辑状态下显示已保存的项目
   		   var projectChecked = [];
   		   <c:forEach items="${projectListCheck}" var ="projectCheck">
   				projectChecked.push("${projectCheck.id}");
	       </c:forEach>
	       $('#projectIds').select2({width:'270px'}).val(projectChecked).trigger("change");
   	   }
   	   
  		$('#projectIds').select2({
   			placeholder: "请选择项目",
   			tags:true,  
   	        createTag:function (decorated, params) {  
   	            return null;  
   	        },  
   			width:'450px'
   		});
   	   
	 });
 	 function objectSubmit(){  
 	 	var url= null;
 	 	var type = $('.typeCode').val();
 	 	var projectIds = $("#projectIds").val();
 	 	if(projectIds==""){
 	 		projectIds = null;
 	 	}
 	 	if($("#wyRole").val()==""){
 	 		$("#wyRole").removeAttr("name");
 	 	}
		if(type==1){//添加实体
			url = path + "/wyAccountController/new"+"/"+projectIds;
		}else{//编辑实体
			url = path + "/wyAccountController/edit"+"/"+projectIds;
		}
     	$.ajax({
		  type: "post",
		  url: url,
		  async:false,
		  data: JSON.stringify($('#objectForm').serializeObject()),
		  dataType: "json",
		  contentType:"application/json",  
		  success: function(result){
		         if(result.status){        	 
		        		$.toast({
		        		    text: result.message,
		        		    hideAfter : 1500,  
		        		    stack : 1,  
		        		    showHideTransition: 'slide',
		        		    icon: 'success',
		        		    textAlign : 'center',
		        		    position : 'top-right'
		        		});
		        	   $('#objectForm').parents('.modal').modal('hide');
		        	   $table.bootstrapTable('refresh');
		         }else{
		        	 $.toast({
		        		    text: result.message,
		        		    hideAfter : 1500,  
		        		    stack : 1,  
		        		    showHideTransition: 'slide',
		        		    icon: 'error',
		        		    textAlign : 'center',
		        		    position : 'top-right'
		        		});
		         }
		  }
         });
      } 
	</script>
	</body>
</html>
